<template>
    <div class="regist">
        <div class="registLeft">
            <img src="../../assets/auth.png">
        </div>
        <div class="registRight">
            <el-form  v-model="regist" :rules="rules" style="width: 400px;padding-top: 40px;"
            ref="numberValidateForm" label-width="100px" class="demo-ruleForm">
        <el-form-item
            label="用户名"
            prop="username"
        >
            <el-input placeholder="用户名：6位数以上的字母和数字组成"  v-model="regist.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
            label="密码"
            prop="password"
        > <el-input type="password" placeholder="密码：至少六位数" v-model="regist.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
            label="学校"
            prop="school"
        > <el-input type="text" placeholder="请输入学校名称" v-model="regist.school" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
            label="昵称"
            prop="nickname"
        > <el-input type="text" placeholder="请输入昵称" v-model="regist.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
            label="年龄"
            prop="age"
        > <el-input type="text" placeholder="请输入年龄，不得超过120岁" v-model="regist.age" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
            label="性别"
            prop="gender"
        > 
         <el-select v-model="regist.gender" placeholder="请选择性别" >
            <el-option label="男" value="0"></el-option>
            <el-option label="女" value="1"></el-option>
        </el-select>
        </el-form-item>
        <el-form-item>已有账号？ 
            <router-link :to="{name:'login'}" >去登陆</router-link>
            </el-form-item>
        <el-form-item>
            <el-button style="width:200px;" type="primary" @click="jump">注册</el-button>
        </el-form-item>
        </el-form>
        </div>
    </div>
</template>
<script>
export default ({
    name:'regist',
    data(){
        let reg = /^[a-zA-Z0-9]{6,16}$/;
        let validateUsername = (rule, value, callback)=>{
            let res = reg.test(this.regist.username)
            if(this.regist.username == null || !res){
                return callback(new Error('请输入正确长度的6-16位英文数字下划线减号组成'))
            }else{
               console.log(this.regist.username)
            }
        }
        let validatePassword = (rule, value, callback)=>{
            let res = reg.test(this.regist.password)
            if(this.regist.password == null || !res){
                return callback(new Error('请输入正确的密码长度应在6-15位英文数字组成'))
            }else{
                console.log(this.regist.password)
            }
        }
        let validateSchool = (rule, value, callback)=>{
            if(this.regist.school == null ){
                return callback(new Error('请输入学校名'))
            }else{
                console.log(this.regist.school)
            }
        }
        let validateNickname = (rule, value, callback)=>{
            if(this.regist.nickname == null ){
                return callback(new Error('请输入昵称'))
            }else{
                console.log(this.regist.nickname)
            }
        }
        let validateAge = (rule, value, callback)=>{
            if(this.regist.age == null || this.regist.age >= 120){
                return callback(new Error('请输入正确的年龄'))
            }else{
                console.log(this.regist.age)
            }
        }
        let validateGender = (rule, value, callback)=>{
            if(this.regist.gender == null ){
                return callback(new Error('请选择性别'))
            }else{
                console.log(this.regist.gender)
            }
        }
        return{
            regist:[],
            rules:{
                username:[
                    { validator: validateUsername, trigger: 'blur' }
                ],
                password:[
                    { validator: validatePassword, trigger: 'blur' }
                ],
                school:[
                    { validator: validateSchool, trigger: 'blur' }
                ],
                nickname:[
                    { validator: validateNickname, trigger: 'blur' }
                ],
                age:[
                    { validator: validateAge, trigger: 'blur' }
                ],
                gender:[
                    { validator: validateGender, trigger: 'blur' }
                ],
            }
        }
    },
    methods:{
       async jump(){
            console.log(this.regist.gender)
            const data = await this.aPost('/user/regist',{
                username:this.regist.username,
                password:this.regist.password,
                school:this.regist.school,
                nickname:this.regist.nickname,
                age:this.regist.age,
                gender:this.regist.gender
            });
            if(data.success == true){
                this.$router.push({
                    name:'login'
                })
            }
        }
    }
})
</script>
<style lang="scss">
.regist{
    width: 880px;
    height: 540px;
    margin: 100px auto;
    box-shadow: 0 0 6px #333;
    display: flex;
    justify-content: space-around;
    img{
        width: 380px;
        height: 460px;
        margin: 40px 10px;
    }
    el-input{
        width: 300px;
    }
}
</style>
